﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using DataEditor.DataLayer;

namespace DataEditor.ViewModels
{
    public class IndicatorTypesViewModel : BaseViewModel
    {
        #region Constructors

        public IndicatorTypesViewModel()
        {
            MakeCommands();
        }

        #endregion

        #region Properties

        private ObservableCollection<IndicatorTypeItemViewModel> _indicatorTypes;

        public ObservableCollection<IndicatorTypeItemViewModel> IndicatorTypes
        {
            get
            {
                _indicatorTypes = new ObservableCollection<IndicatorTypeItemViewModel>();
                var query = from i in dataContext.INDICATOR_TYPE
                            select i;
                foreach (var item in query)
                {
                    _indicatorTypes.Add(new IndicatorTypeItemViewModel(item));
                }
                return _indicatorTypes;
            }
            set
            {
                _indicatorTypes = value;
                NotifyOfPropertyChange(() => IndicatorTypes);
            }
        }

        private IndicatorTypeItemViewModel _selectedIndicatorType;

        public IndicatorTypeItemViewModel SelectedIndicatorType
        {
            get { return _selectedIndicatorType; }
            set
            {
                _selectedIndicatorType = value;
                NotifyOfPropertyChange(() => SelectedIndicatorType);
            }
        }


        RelayCommand _addNewCommand;
        public RelayCommand AddNewCommand
        {
            get { return _addNewCommand; }
            set
            {
                _addNewCommand = value;
                NotifyOfPropertyChange(() => AddNewCommand);
            }
        }

        RelayCommand _removeCommand;
        public RelayCommand RemoveCommand
        {
            get { return _removeCommand; }
            set
            {
                _removeCommand = value;
                NotifyOfPropertyChange(() => RemoveCommand);
            }
        }

        #endregion

        #region Action/methods

        private void MakeCommands()
        {
            _addNewCommand = new RelayCommand(
                   param =>
                   {
                       INDICATOR_TYPE it = new INDICATOR_TYPE()
                       {
                           NAME = String.Empty,
                           DESCRIPTION = String.Empty,
                           IsValid = true,
                           ValidFrom = DateTime.Now
                       };
                       dataContext.INDICATOR_TYPE.AddObject(it);
                       dataContext.SaveChanges();
                       IndicatorTypes.Add(new IndicatorTypeItemViewModel(it));
                       NotifyOfPropertyChange(() => IndicatorTypes);
                   }
               );

            _removeCommand = new RelayCommand(
                param =>
                {
                    dataContext.INDICATOR_TYPE.DeleteObject(SelectedIndicatorType.Model);
                    dataContext.SaveChanges();
                    IndicatorTypes.Remove(SelectedIndicatorType);
                    NotifyOfPropertyChange(() => IndicatorTypes);
                },
                    (obj) =>
                    {
                        return SelectedIndicatorType != null;
                    }
            );
        }

        public void Save()
        {
            foreach (var item in _indicatorTypes)
            {
            }
            foreach (var item in dataContext.INDICATOR_TYPE)
            {
                IndicatorTypeItemViewModel vm = _indicatorTypes.FirstOrDefault(t=> t.Model == item);
                if (vm != null)
                {
                    item.IsValid = vm.IsValid;
                    item.ValidFrom = vm.ValidFrom;
                    item.ValidTo = vm.ValidTo;
                }
            }
            dataContext.SaveChanges();
        }

        #endregion
    }
}
